package com.cx.service.impl;

import com.cx.mapper.HotelMapper;
import com.cx.pojo.HotelComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import com.cx.tool.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentDao {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    HotelMapper hotelMapper;
    /**
     *
     * @return
     */
    public List<HotelComment> findAllComment(){
        List<HotelComment> comments = mongoTemplate.findAll(HotelComment.class,"hotel_comment");
        int num = comments.size();
        if(comments.size() != 0){
            comments.get(0).count = num;
        }
        return comments;//这个comment_hotel是存储的MongoDB表名
    }

    /**
     * 保存
     * @param hotelComment
     */
    public void save(HotelComment hotelComment){
        mongoTemplate.save(hotelComment,"hotel_comment");
    }

    public List<HotelComment> findHotelCommentByHotelId(Integer id,int pageNUm){
        Criteria criteria =  Criteria.where("hotelId").in(id);
//        BasicDBObject basicDBObject = new BasicDBObject();
        Query query = new Query(criteria).limit(5).skip(5 * (pageNUm - 1));
        return mongoTemplate.find(query,HotelComment.class,"hotel_comment");
    }

    public List<HotelComment> gettRecentCommentByHotelId(Integer id){
        Criteria criteria =  Criteria.where("hotelId").in(id);
        Query query = new Query(criteria);
        List<HotelComment> comments = mongoTemplate.find(query,HotelComment.class,"hotel_comment");
        int length = comments.size();
        if(length >= 2){
            return mongoTemplate.find(query,HotelComment.class,"hotel_comment").subList(length-2,length);
        }
        return null;
    }
    public Integer findCommentsCountByHotelId(Integer id){
        Criteria criteria =  Criteria.where("hotelId").in(id);
        Query query = new Query(criteria);
        List<HotelComment> comments = mongoTemplate.find(query,HotelComment.class,"hotel_comment");
        return comments.size();
    }
    public void updateHotelScore(Integer id){
        List<Integer> hotelScore = new ArrayList<>();
        Criteria criteria =  Criteria.where("hotelId").in(id);
        Query query = new Query(criteria);
        List<HotelComment> comments = mongoTemplate.find(query,HotelComment.class,"hotel_comment");
//        System.out.println(comments.get(0).getHotelScore() + "挤兑俺");
        for(HotelComment hotelComment : comments){
            hotelScore.add(hotelComment.getHotelScore());
        }
        com.cx.tool.hotelScore hotelScore1 = new hotelScore();
        Double avgScroe = hotelScore1.updateHotelScoreTool(hotelScore);
        //保留一位小数
        double result = new BigDecimal(avgScroe).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
        if (comments.size() > 0) {
            hotelMapper.updateHotelScore(id, result);
        }
        System.out.println(result);
    }
    public List<HotelComment> findGoodComment(){
        System.out.println("ttttttttttttt");
        Criteria criteria =  Criteria.where("isGoodComment").is(true);
        Query query = new Query(criteria);
        List<HotelComment> comments = mongoTemplate.find(query,HotelComment.class,"hotel_comment");
        System.out.println(comments);
        return comments;
    }

}
